package org.erp.procurement.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.erp.procurement.constant.OrderConstant;
import org.erp.procurement.entity.MiniOrder;
import org.erp.procurement.entity.PcOrder;
import org.erp.procurement.enums.OrderEventEnum;
import org.erp.procurement.enums.OrderStatusEnum;
import org.erp.procurement.exception.ProcurementException;
import org.erp.procurement.mapper.MiniOrderMapper;
import org.erp.procurement.mapper.PcOrderMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.persist.DefaultStateMachinePersister;
import org.springframework.statemachine.persist.StateMachinePersister;
import org.springframework.statemachine.support.DefaultStateMachineContext;

/**
 * 订单状态持久化配置
 *
 * @author libin
 * @version 1.0
 * @description: 订单状态持久化配置
 * @date 2025/11/10 0:37
 **/
@Log4j2
@Configuration
@AllArgsConstructor
public class OrderStateMachinePersist {

    private final MiniOrderMapper miniOrderMapper;

    private final PcOrderMapper pcOrderMapper;

    @Bean
    public StateMachinePersister<OrderStatusEnum, OrderEventEnum, String> orderStateMachinePersister() {
        return new DefaultStateMachinePersister<>(new StateMachinePersist<OrderStatusEnum, OrderEventEnum, String>() {
            @Override
            public void write(StateMachineContext<OrderStatusEnum, OrderEventEnum> context, String orderNo) throws Exception {
                log.info("当前采购订单为: {},更新后状态为: {}", orderNo, context.getState().getMessage());
                if (orderNo.startsWith(OrderConstant.MINI_PREFIX)) {
                    MiniOrder miniOrder = miniOrderMapper.selectOne(new QueryWrapper<MiniOrder>().lambda()
                            .eq(MiniOrder::getOrderNo, orderNo));
                    if (ObjectUtils.isEmpty(miniOrder)) {
                        throw new ProcurementException("订单不存在: " + orderNo);
                    }
                    miniOrder.setStatus(context.getState().getCode());
                    miniOrderMapper.updateById(miniOrder);
                } else if (orderNo.startsWith(OrderConstant.PC_PREFIX)) {
                    PcOrder pcOrder = pcOrderMapper.selectOne(new QueryWrapper<PcOrder>().lambda()
                            .eq(PcOrder::getOrderNo, orderNo));
                    if (ObjectUtils.isEmpty(pcOrder)) {
                        throw new ProcurementException("订单不存在: " + orderNo);
                    }
                    pcOrder.setStatus(context.getState().getCode());
                    pcOrderMapper.updateById(pcOrder);
                }
            }

            @Override
            public StateMachineContext<OrderStatusEnum, OrderEventEnum> read(String orderNo) throws Exception {
                OrderStatusEnum orderStatusEnum = null;
                if (orderNo.startsWith(OrderConstant.MINI_PREFIX)) {
                    MiniOrder miniOrder = miniOrderMapper.selectOne(new QueryWrapper<MiniOrder>().lambda()
                            .eq(MiniOrder::getOrderNo, orderNo));
                    if (ObjectUtils.isEmpty(miniOrder)) {
                        throw new ProcurementException("订单不存在: " + orderNo);
                    }
                    orderStatusEnum = OrderStatusEnum.getByCode(miniOrder.getStatus());
                } else if (orderNo.startsWith(OrderConstant.PC_PREFIX)) {
                    PcOrder pcOrder = pcOrderMapper.selectOne(new QueryWrapper<PcOrder>().lambda()
                            .eq(PcOrder::getOrderNo, orderNo));
                    if (ObjectUtils.isEmpty(pcOrder)) {
                        throw new ProcurementException("订单不存在: " + orderNo);
                    }
                    orderStatusEnum = OrderStatusEnum.getByCode(pcOrder.getStatus());
                } else {
                    throw new ProcurementException("订单前缀不合法: " + orderNo);
                }
                return new DefaultStateMachineContext<>(orderStatusEnum, null, null, null, null, orderNo);
            }
        });
    }
}
